home *** CD-ROM | disk | FTP | other *** search
/ MacGames Sampler / PHT MacGames Bundle.iso / MacSource Folder / Samples from the CD / C and C++ / POSIX / ThinkCPosix / alloca.c < prev    next >
Text File  |  1992-09-14  |  6KB  |  226 lines

  1. /* $Id: $ */
  2.  
  3. /*
  4.     alloca -- (mostly) portable public-domain implementation -- D A Gwyn
  5.  
  6.     last edit:    86/05/30    rms
  7.        include config.h, since on VMS it renames some symbols.
  8.        Use xmalloc instead of malloc.
  9.  
  10.     This implementation of the PWB library alloca() function,
  11.     which is used to allocate space off the run-time stack so
  12.     that it is automatically reclaimed upon procedure exit, 
  13.     was inspired by discussions with J. Q. Johnson of Cornell.
  14.  
  15.     It should work under any C implementation that uses an
  16.     actual procedure stack (as opposed to a linked list of
  17.     frames).  There are some preprocessor constants that can
  18.     be defined when compiling for your specific system, for
  19.     improved efficiency; however, the defaults should be okay.
  20.  
  21.     The general concept of this implementation is to keep
  22.     track of all alloca()-allocated blocks, and reclaim any
  23.     that are found to be deeper in the stack than the current
  24.     invocation.  This heuristic does not reclaim storage as
  25.     soon as it becomes invalid, but it will do so eventually.
  26.  
  27.     As a special case, alloca(0) reclaims storage without
  28.     allocating any.  It is a good idea to use alloca(0) in
  29.     your main control loop, etc. to force garbage collection.
  30. */
  31. #ifndef lint
  32. static char    SCCSid[] = "@(#)alloca.c    1.1";    /* for the "what" utility */
  33. #endif
  34.  
  35. #ifdef THINK_C
  36. #pragma once
  37. #include <stdlib.h>
  38. #include "ThinkCPosix.h"
  39. void *alloca (unsigned);
  40. static void *xmalloc (unsigned);
  41. #endif /* THINK_C */
  42.  
  43. #ifdef emacs
  44. #include "config.h"
  45. /* THINK_C chokes on following line, even though emacs is not defined
  46. #ifdef static
  47. */
  48. /* actually, only want this if static is defined as ""
  49.    -- this is for usg, in which emacs must undefine static
  50.    in order to make unexec workable
  51.    */
  52. #ifndef STACK_DIRECTION
  53. you
  54. lose
  55. -- must know STACK_DIRECTION at compile-time
  56. #endif /* STACK_DIRECTION undefined */
  57. /*
  58. #endif /* static */
  59. #endif /* emacs */
  60.  
  61. #ifndef alloca  /* If compiling with GCC, this file's not needed.  */
  62.  
  63. #ifdef __STDC__
  64. typedef void    *pointer;        /* generic pointer type */
  65. #else
  66. typedef char    *pointer;        /* generic pointer type */
  67. #endif
  68.  
  69. #ifndef NULL
  70. #define    NULL    0            /* null pointer constant */
  71. #endif
  72.  
  73. extern void    free();
  74. extern pointer    xmalloc();
  75.  
  76. /*
  77.     Define STACK_DIRECTION if you know the direction of stack
  78.     growth for your system; otherwise it will be automatically
  79.     deduced at run-time.
  80.  
  81.     STACK_DIRECTION > 0 => grows toward higher addresses
  82.     STACK_DIRECTION < 0 => grows toward lower addresses
  83.     STACK_DIRECTION = 0 => direction of growth unknown
  84. */
  85.  
  86. #ifndef STACK_DIRECTION
  87. #define    STACK_DIRECTION    0        /* direction unknown */
  88. #endif
  89.  
  90. #if STACK_DIRECTION != 0
  91.  
  92. #define    STACK_DIR    STACK_DIRECTION    /* known at compile-time */
  93.  
  94. #else    /* STACK_DIRECTION == 0; need run-time code */
  95.  
  96. static int    stack_dir;        /* 1 or -1 once known */
  97. #define    STACK_DIR    stack_dir
  98.  
  99. static void
  100. find_stack_direction (/* void */)
  101. {
  102.   static char    *addr = NULL;    /* address of first
  103.                    `dummy', once known */
  104.   auto char    dummy;        /* to get stack address */
  105.  
  106.   if (addr == NULL)
  107.     {                /* initial entry */
  108.       addr = &dummy;
  109.  
  110.       find_stack_direction ();    /* recurse once */
  111.     }
  112.   else                /* second entry */
  113.     if (&dummy > addr)
  114.       stack_dir = 1;        /* stack grew upward */
  115.     else
  116.       stack_dir = -1;        /* stack grew downward */
  117. }
  118.  
  119. #endif    /* STACK_DIRECTION == 0 */
  120.  
  121. /*
  122.     An "alloca header" is used to:
  123.     (a) chain together all alloca()ed blocks;
  124.     (b) keep track of stack depth.
  125.  
  126.     It is very important that sizeof(header) agree with malloc()
  127.     alignment chunk size.  The following default should work okay.
  128. */
  129.  
  130. #ifndef    ALIGN_SIZE
  131. #define    ALIGN_SIZE    sizeof(double)
  132. #endif
  133.  
  134. typedef union hdr
  135. {
  136.   char    align[ALIGN_SIZE];    /* to force sizeof(header) */
  137.   struct
  138.     {
  139.       union hdr *next;        /* for chaining headers */
  140.       char *deep;        /* for stack depth measure */
  141.     } h;
  142. } header;
  143.  
  144. /*
  145.     alloca( size ) returns a pointer to at least `size' bytes of
  146.     storage which will be automatically reclaimed upon exit from
  147.     the procedure that called alloca().  Originally, this space
  148.     was supposed to be taken from the current stack frame of the
  149.     caller, but that method cannot be made to work for some
  150.     implementations of C, for example under Gould's UTX/32.
  151. */
  152.  
  153. static header *last_alloca_header = NULL; /* -> last alloca header */
  154.  
  155. pointer
  156. alloca (size)            /* returns pointer to storage */
  157.      unsigned    size;        /* # bytes to allocate */
  158. {
  159.   auto char    probe;        /* probes stack depth: */
  160.   register char    *depth = &probe;
  161.  
  162. #if STACK_DIRECTION == 0
  163.   if (STACK_DIR == 0)        /* unknown growth direction */
  164.     find_stack_direction ();
  165. #endif
  166.  
  167.                 /* Reclaim garbage, defined as all alloca()ed storage that
  168.                    was allocated from deeper in the stack than currently. */
  169.  
  170.   {
  171.     register header    *hp;    /* traverses linked list */
  172.  
  173.     for (hp = last_alloca_header; hp != NULL;)
  174.       if ((STACK_DIR > 0 && hp->h.deep > depth)
  175.       || (STACK_DIR < 0 && hp->h.deep < depth))
  176.     {
  177.       register header    *np = hp->h.next;
  178.  
  179.       free ((pointer) hp);    /* collect garbage */
  180.  
  181.       hp = np;        /* -> next header */
  182.     }
  183.       else
  184.     break;            /* rest are not deeper */
  185.  
  186.     last_alloca_header = hp;    /* -> last valid storage */
  187.   }
  188.  
  189.   if (size == 0)
  190.     return NULL;        /* no allocation required */
  191.  
  192.   /* Allocate combined header + user data storage. */
  193.  
  194.   {
  195.     register pointer    new = xmalloc (sizeof (header) + size);
  196.     /* address of header */
  197.  
  198.     ((header *)new)->h.next = last_alloca_header;
  199.     ((header *)new)->h.deep = depth;
  200.  
  201.     last_alloca_header = (header *)new;
  202.  
  203.     /* User storage begins just after header. */
  204.  
  205.     return (pointer)((char *)new + sizeof(header));
  206.   }
  207. }
  208.  
  209. #ifdef THINK_C
  210. static void*
  211. xmalloc (unsigned size)
  212. {
  213.   void *new_mem = (void*) malloc (size);
  214.  
  215.   if (new_mem == NULL)
  216.     {
  217.       fprintf (stderr, "xmalloc: request for %u bytes failed.\n", size);
  218.       abort ();
  219.     }
  220.  
  221.   return new_mem;
  222. }
  223. #endif /* THINK_C */
  224.  
  225. #endif /* no alloca */
  226.